package com.guigu.qxl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.pojo.*;
import com.guigu.qxl.pojo.*;
import com.guigu.qxl.mapper.MDesignProcedureMapper;
import com.guigu.qxl.pojo.MDesignProcedureModuleVo;
import com.guigu.qxl.pojo.MDesignProcedureVo;
import com.guigu.qxl.service.MDesignProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MDesignProcedureServiceImpl implements MDesignProcedureService {
    @Autowired
    MDesignProcedureMapper mDesignProcedureMapper;

    @Override
    public PageInfo<QMDesignProcedureDetails> showAllMdesign(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedureDetails> list=new ArrayList<QMDesignProcedureDetails>();
        QMDesignProcedureDetails m1=new QMDesignProcedureDetails();
        m1.setProcedureId("01");
        m1.setProcedureName("组装");
        m1.setProcedureDescribe("");
        list.add(m1);
        QMDesignProcedureDetails m2=new QMDesignProcedureDetails();
        m2.setProcedureId("02");
        m2.setProcedureName("测试");
        m2.setProcedureDescribe("");
        list.add(m2);
        QMDesignProcedureDetails m3=new QMDesignProcedureDetails();
        m3.setProcedureId("03");
        m3.setProcedureName("包装");
        m3.setProcedureDescribe("");
        list.add(m3);
        return new PageInfo<QMDesignProcedureDetails>(list);
    }




    @Override
    public boolean MDesignProcedureRegister(MDesignProcedureVo mDesignProcedureVo){
        String material=mDesignProcedureMapper.showProcessnumber();
        String[] arr1 =mDesignProcedureVo.getRegisterTime().split(" ");
        String[] arr2 =arr1[0].split("/");
        String designId="201";
        int a1=Integer.parseInt("100"+material)+1;
        String b=""+a1;
        b=b.substring(3);
        int c=mDesignProcedureMapper.updProcessnumber(b);
        for(String a:arr2){
            if(a.length()==1){
                a="0"+a;
            }
            designId=designId+a;
        }
        designId=designId+material;
        mDesignProcedureVo.setDesignId(designId);
        int n=mDesignProcedureMapper.register(mDesignProcedureVo);
        int id=mDesignProcedureVo.getId();
        for(QMDesignProcedureDetails d:mDesignProcedureVo.getList()){
            d.setParentId(id);
            d.setRegister(mDesignProcedureVo.getRegister());
            d.setRegisterTime(mDesignProcedureVo.getRegisterTime());
            mDesignProcedureMapper.insertDetails(d);
        }
        mDesignProcedureMapper.updateProcedure(mDesignProcedureVo.getProductId());
        return n==1;
    }

    @Override
    public boolean mDesignProcedureModuleRegister(MDesignProcedureModuleVo mdesignProcedureModuleVo){
        int sum=0;
        int parentId=mdesignProcedureModuleVo.getList().get(0).getParentId();
        int id=mDesignProcedureMapper.selectByProductId(mdesignProcedureModuleVo.getProductId());
        for(QMDesignProcedureModule m:mdesignProcedureModuleVo.getList()){
            mDesignProcedureMapper.insertModule(m);
            sum+=m.getSubtotal();
            QDModuleDetails d=new QDModuleDetails();
            d.setParentId(id);
            d.setProductId(m.getProductId());
            d.setResidualAmount(m.getAmount());
            mDesignProcedureMapper.updnumber(d);
        }
        QMDesignProcedureDetails mdpd=new QMDesignProcedureDetails();
        mdpd.setId(parentId);
        mdpd.setModuleSubtotal(sum);
        mdpd.setDesignModuleTag("D002-1");
        int n=mDesignProcedureMapper.updateProcedureDetails(mdpd);
        return n==1;
    }

    @Override
    public PageInfo<QMDesignProcedure> pageS0010MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageS0010MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageS0011MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageS0011MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageS0011G0020MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageS0011G0020MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageS0011G0022MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageS0011G0022MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageG0020MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageG0020MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageS0011G0021MDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageS0011G0021MDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public PageInfo<QMDesignProcedure> pageMDesignProcedure(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<QMDesignProcedure> list= mDesignProcedureMapper.pageMDesignProcedure();
        return new PageInfo<QMDesignProcedure>(list);
    }

    @Override
    public QMDesignProcedure showMDesignProcedureById(QMDesignProcedure mdesignProcedure){
        return mDesignProcedureMapper.selectByPrimaryKey(mdesignProcedure.getId());
    }

    @Override
    public QMDesignProcedureDetails showMDesignProcedureDetailsById(QMDesignProcedureDetails mdesignProcedureDetails){
        return mDesignProcedureMapper.showMDesignProcedureDetailsById(mdesignProcedureDetails.getId());
    }

    @Override
    public List<QMDesignProcedureDetails> showMDesignProcedureDetailsByParentId(QMDesignProcedure mdesignProcedure){
        return mDesignProcedureMapper.showMDesignProcedureDetailsByParentId(mdesignProcedure.getId());
    }

    @Override
    public boolean DesignModuleCheckOk(QMDesignProcedure mdesignProcedure){
        String productId=mdesignProcedure.getProductId();
        if(mdesignProcedure.getCheckTag()=="S001-2"){
            mDesignProcedureMapper.UpdDesignModuleDesignProcedure(productId);
            int id=mDesignProcedureMapper.selectByProductId(productId);
            mDesignProcedureMapper.delModuleDetailsById(id);
            mDesignProcedureMapper.delDesignModule(productId);
        }
        return mDesignProcedureMapper.DesignModuleCheckOk(mdesignProcedure)==1;
    }

    @Override
    public boolean mdesignProcedureCheckOk(QMDesignProcedure mdesignProcedure){
        if(mdesignProcedure.getCheckTag().equals("S001-2")){
            mDesignProcedureMapper.updateNoProcedure(mdesignProcedure.getProductId());
        }
        return mDesignProcedureMapper.mdesignProcedureCheckOk(mdesignProcedure)==1;
    }

    @Override
    public boolean mdesignProcedureChange(MDesignProcedureVo mdesignProcedureVo) {
        int n=mDesignProcedureMapper.mdesignProcedureChange(mdesignProcedureVo);
        mDesignProcedureMapper.deleteDetailsByParentId(mdesignProcedureVo.getId());
        for(QMDesignProcedureDetails d:mdesignProcedureVo.getList()){
            d.setParentId(mdesignProcedureVo.getId());
            mDesignProcedureMapper.insertDetails(d);
        }
        return n==1;
    }

    @Override
    public List<QDModuleDetails> showProuctById(String productId){
        int id=mDesignProcedureMapper.selectByProductId(productId);
        return mDesignProcedureMapper.showProuctById(id);
    }

    @Override
    public void removeById(String productId, int id){
        int parentId=mDesignProcedureMapper.selectByProductId(productId);
        List<QMDesignProcedureModule> list=mDesignProcedureMapper.showProcedureModuleByParentId(id);
        mDesignProcedureMapper.updateProcedureDetailsById(id);
        mDesignProcedureMapper.removeProcedureModuleById(id);
        for(QMDesignProcedureModule d:list){
            mDesignProcedureMapper.updateModuleDetails(parentId,d.getProductId(),d.getAmount());
        }
    }

    @Override
    public boolean UpdMDesignProcedure(QMDesignProcedure mdesignProcedure){
        return mDesignProcedureMapper.UpdMDesignProcedure(mdesignProcedure);
    }

    @Override
    public List<QMDesignProcedureModule> showProcedureModuleByParentId(int id){
        return mDesignProcedureMapper.showProcedureModuleByParentId(id);
    }

    @Override
    public QDFile showDFileByProductId(String productId){
        return mDesignProcedureMapper.showDFileByProductId(productId);
    }
}
